package com.uchengwang.ui.cm_ui.ucity.presenter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UCityConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.AppUpdateBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.UcityBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.UcityInfoBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.UserBeanList;
import com.uchengwang.ui.cm_ui.ucity.listener.GetApplyListBeanListener;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.Date;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2017/5/24.
 */

public class HttpCityPresenter {

    public static HttpCityPresenter presenter;

    private HttpCityPresenter(){}

    public static HttpCityPresenter getPresenter() {
        if (presenter == null){
            presenter = new HttpCityPresenter();
        }
        return presenter;
    }

    /**
     * @param page 请求页数
     * @param type 请求类型 1-U学，2-U赛，3-U行，4-U用，5-U公益活动倡议，6-U公益失物招领,7-社团活动, 8-社团分享 9-热门帖子 10发布的头条
     * @param isAddUniId 是否需要带上学校的ID
     * */
    public void getUcityInfos(Activity activity, int page, int type, boolean isAddUniId, final GetUcityInfosListener listener){
        MapRequest request = new MapRequest();
        if (UChengApplication.uniId >0 && isAddUniId){
            request.put(BaseConfig.UNI_ID, UChengApplication.uniId);
        }
        request.put(BaseConfig.PAGE, page);
        request.put(BaseConfig.PERPAGE, 10);
        request.put(BaseConfig.TYPE, type);

        getInfos(activity, request, listener);
    }
    /**
     * 4U 获取数据
     * @param page 请求页数
     * @param type 请求类型 1-U学，2-U赛，3-U行，4-U用，5-U公益活动倡议，6-U公益失物招领,7-社团活动, 8-社团分享)
     * @param isAddUniId 是否需要添加uniId
     * */
    public void getUcityInfos(Activity activity, int page, int type, int pubItemType,  boolean isAddUniId, final GetUcityInfosListener listener){
        MapRequest request = new MapRequest();
        if (UChengApplication.uniId >0 && isAddUniId){
            request.put(BaseConfig.UNI_ID, UChengApplication.uniId);
        }
        request.put(BaseConfig.PAGE, page);
        request.put(BaseConfig.PERPAGE, 10);
        request.put(BaseConfig.TYPE, type);

        switch (type){
            case BaseConfig.U_STUDY:
            case BaseConfig.U_GAME:
            case BaseConfig.U_WALK:
                Date now=new Date();
                long start=now.getTime();
                long mi = 30*24*60*60*1000L;
                long end = start + mi;
                request.put(BaseConfig.START_TIME, start);
                request.put(BaseConfig.END_TIME, end);
                break;

        }

        if (pubItemType > 0){
            //筛选全部 传0
            request.put(BaseConfig.PUB_ITEM_TYPE, pubItemType);
        }

        getInfos(activity, request, listener);
    }

    /**
     * 获取校园热门
     * @param isHot 热门与否
     * */
    public void getUcityHotInfos(Activity activity, boolean isHot, final GetUcityInfosListener listener){
        MapRequest request = new MapRequest();
//        if (UChengApplication.uniId >0){
//            request.put(BaseConfig.UNI_ID, UChengApplication.uniId);
//        }
        request.put(BaseConfig.PAGE, 1);
        request.put(BaseConfig.PERPAGE, 10);
        request.put(BaseConfig.IS_HOT, isHot);

        getInfos(activity, request, listener);
    }

    /**
     * 获取固定类型和个数的校园热门
     * @param isHot 热门与否
     *@param type 类型
     *@param perPage 个数
     *@param isAddUniID 是否带学校的ID
     * */
    public void getTypeUcityHotInfos(Activity activity,int type,  int perPage, boolean isHot, boolean isAddUniID,  final GetUcityInfosListener listener){
        MapRequest request = new MapRequest();
        request.put(BaseConfig.TYPE, type);
        request.put(BaseConfig.PAGE, 1);
        request.put(BaseConfig.PERPAGE, perPage);
        switch (type){
            case BaseConfig.U_LOST:
                if (UChengApplication.uniId >0 && isAddUniID){
                    request.put(BaseConfig.UNI_ID, UChengApplication.uniId);
                }
                break;
            case BaseConfig.U_BENEFIT:
//                request.put(BaseConfig.IS_HOT, isHot);
                if (UChengApplication.uniId >0 && isAddUniID){
                    request.put(BaseConfig.UNI_ID, UChengApplication.uniId);
                }
                break;
        }


        getInfos(activity, request, listener);
    }



    /**
     * @param page 请求页数
     * @param type 请求类型 1-U学，2-U赛，3-U行，4-U用，5-U公益活动倡议，6-U公益失物招领,7-社团活动, 8-社团分享)
     * @param checkId 查看某人/社团的发布
     * */
    public void getUcityInfos(Activity activity, int page, int type,  long checkId, boolean isAdd, final GetUcityInfosListener listener){
        MapRequest request = new MapRequest();
        if (UChengApplication.uniId >0 && isAdd){
            request.put(BaseConfig.UNI_ID, UChengApplication.uniId);
        }
        request.put(BaseConfig.PAGE, page);
        request.put(BaseConfig.PERPAGE, 10);
        if (type >0){
            request.put(BaseConfig.TYPE, type);
        }
        if (checkId >0){
            request.put(BaseConfig.CHECK_UID, checkId);
        }

        getInfos(activity, request, listener);
    }

    /**
     * 获取热门帖子
     * */
    public void getHotArticle(Activity activity, int page, final GetUcityInfosListener listener){
        MapRequest request = new MapRequest();
        request.put(BaseConfig.PAGE, page);
        request.put(BaseConfig.PERPAGE, 10);
        request.put(BaseConfig.TYPE, BaseConfig.U_SCHOOL_ARTICLE);
//        request.put(BaseConfig.IS_HOT, true);

        getInfos(activity, request, listener);
    }


    public void getInfos(Activity activity, MapRequest mapRequest, final GetUcityInfosListener listener){
        //U行 U赛 U用请求方式一样
        HttpManager.newInstance(activity).query(UCityConfig.Ucity.methos_get, UCityConfig.Ucity.URL, mapRequest, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, 0,  msg, null);
            }

            @Override
            public void onResp(int status, String msg,  String data) {
                if (status != 0){
                    //请求不成功
                    listener.onResult(status, 0, msg, null);
                    return;
                }
                UcityBean bean = JsonUtils.getManager().stringToBean(data, UcityBean.class);
                if (bean != null){
                    int allPagers = bean.getTotalPage();
                    List<UcityInfoBean> list = bean.getData();
                    if (list != null && list.size() >0){
                        listener.onResult(status, allPagers, msg, list);
                    }else {
                        listener.onResult(status, allPagers, msg, null);
                    }
                }else {
                    listener.onResult(status,0,  msg, null);
                }


            }
        });
    }


    /**
     * 请求查看报名人数 因为只先请求4个用于显示
     * @param id 此条信息的ID
     * @param page 当前页
     * @param perpage 每页个数
     * @param isAccept 是否筛选已同意或未同意,不填则显示全部
     * */
    public void getApplyInfos(Activity activity, long id ,int page, int perpage, Boolean isAccept,  final GetApplyListBeanListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, id);
        map.put(BaseConfig.PAGE, page);
        if (isAccept != null){
            map.put(BaseConfig.IS_ACCEPT, isAccept);
        }
        map.put(BaseConfig.PERPAGE, perpage);
        HttpManager.newInstance(activity).query(UCityConfig.UcityEnroll.method_check, UCityConfig.UcityEnroll.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, -1,-1,  msg, null);
            }

            @Override
            public void onResp(int status, String msg,  String data) {
                if (status != 0){
                    listener.onResult(status, -1,-1,  msg, null);
                    return;
                }
                if (!TextUtils.isEmpty(data)){
                    UserBeanList bean = JsonUtils.getManager().stringToBean(data, UserBeanList.class);
                    List<UserBean> beanList = bean.getData();
                    if (beanList != null && beanList.size() > 0){
                        listener.onResult(status, bean.getTotalPage(),bean.getTotal(),  msg, beanList);
                    }else {
                        listener.onResult(status, bean.getTotalPage(),bean.getTotal(),  msg, null);
                    }
                }

            }
        });
    }

    /**
     * 获取某一条U城信息详情
     * @param id 某条信息的ID
     * */
    public void getUCityInfo(Activity activity, long id, final GetUcityInfoListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, id);
        HttpManager.newInstance(activity).query(UCityConfig.Ucity.methos_get, UCityConfig.Ucity.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("banner", "失物招领="+"code="+status+"=msg="+msg);
                listener.onResult(status, msg, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("banner", "失物招领="+"code="+status+"=msg="+msg);
                if (status != 0){
                    listener.onResult(status, msg, null);
                    return;
                }

                if (!TextUtils.isEmpty(data)){
                    UcityInfoBean ucityInfoBean = JsonUtils.getManager().stringToBean(data, UcityInfoBean.class);
                    listener.onResult(status, msg, ucityInfoBean);
                }else {
                    listener.onResult(status, msg, null);
                }


            }
        });
    }

    /**
     * 获取某条U城信息 跳转相应界面
     * */
    public void ucityOnClick(final Activity activity, long id){
        LoadingUtils.getUtils().loading(activity);
        getUCityInfo(activity, id, new GetUcityInfoListener() {
            @Override
            public void onResult(int status, String msg, UcityInfoBean ucityInfoBean) {
                LoadingUtils.getUtils().hide();
                if (ucityInfoBean != null){
                    int type = ucityInfoBean.getPubType();

                    //1-U学，2-U赛，3-U行，4-U用，5-U公益活动倡议，6-U公益失物招领,7-社团活动, 8-社团分享)  9校吧帖子
                    Intent intent = null;
                    switch (type){
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case BaseConfig.U_BOOK:
//                            intent = new Intent(activity, UCity3Activity.class);
//                            intent.putExtra(BaseConfig.ID, ucityInfoBean.getId());
//                            intent.putExtra(BaseConfig.PUB_TYPE, type);
                            break;

                        case 6:
                            break;
                        case 5:
                        case 8:
                        case 9:
//                            intent = new Intent(activity, SchoolNewDetailActivity.class);
//                            intent.putExtra(BaseConfig.ID, ucityInfoBean.getId());

                            break;
                    }
                    if (intent != null){
                        activity.startActivity(intent);
                    }
                }else {
                    UCommonUtil.getUtil().showToast(activity, "获取信息失败");
                }
            }
        });
    }


    /**
     * 撤回
     * */
    public void withdraw(final Activity activity, final String withdrawReason, final long id , final HttpCallBack callBack){
        withdrawOrDelete(activity, true, withdrawReason, id, callBack);
    }

    /**
     * 删除
     * */
    public void withdrawDelete(final Activity activity,  final long id , final HttpCallBack callBack){
        withdrawOrDelete(activity, false, "", id, callBack);
    }

    /**
     * 撤回/删除 一条信息
     * @param id 消息的ID
     * @param isWithdraw true 撤回 false 删除
     * @param withdrawReason 撤回理由
     * */
    private AlertDialog dialog;
    private void withdrawOrDelete(final Activity activity, final boolean isWithdraw, final String withdrawReason, final long id , final HttpCallBack callBack){
        String title = "";
        if (isWithdraw){
            title = "撤回";
        }else {
            title = "删除";
        }


        final String finalTitle = title;
        dialog = DialogUtils.getUtils().showDialogAll(activity, false, "", title+"不可取消!", "取消", title, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoadingUtils.getUtils().loading(activity);
                dialog.cancel();
                MapRequest map = new MapRequest();
                map.put(BaseConfig.UPDATE_ID, id);
                if (isWithdraw){
                    map.put(BaseConfig.IS_ACTIVI, false);
                    if (!TextUtils.isEmpty(withdrawReason)){
                        map.put(BaseConfig.RECALL_REASON, withdrawReason);
                    }
                }else {
                    map.put(BaseConfig.USER_DELETE, true);
                }
                HttpManager.newInstance(activity).query(UCityConfig.Ucity.method_put, UCityConfig.Ucity.URL, map, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        LoadingUtils.getUtils().hide();
                        Log.d("jjf", "status="+status+"=msg="+msg);
                        if (callBack != null){
                            callBack.onResp(status, msg, null);
                        }
                        UCommonUtil.getUtil().showToast(activity, finalTitle+"失败");
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        LoadingUtils.getUtils().hide();
                        Log.d("jjf", "status="+status+"=msg="+msg);

                        if (status != 0){
                            UCommonUtil.getUtil().showToast(activity, msg);
                            if (callBack != null){
                                callBack.onResp(status, msg, null);
                            }
                            return;
                        }
                        UCommonUtil.getUtil().showToast(activity, finalTitle +"成功");
                        if (callBack != null){
                            callBack.onResp(status, msg, null);
                        }else {
                            UcityInfoBean b = new UcityInfoBean();
                            b.setId(id);
                            if (isWithdraw){
                                b.setUserDelete(false);
                            }else {
                                b.setUserDelete(true);
                            }
                            EventBus.getDefault().post(b);

                            BaseConfig.isRelease = true;
                            activity.finish();
                        }


                    }
                });
            }
        }, null);
    }

    /**
     * 检查版本更新
     * @param versionCode 当前版本号
     * */
    public void sysVersion(final Activity activity, String versionCode){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.VERSION_CODE, versionCode);
        HttpManager.newInstance(activity).queryCheck(UCityConfig.SysVersion.method, UCityConfig.SysVersion.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "版本stutas="+status+"=msg="+msg);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "版本stutas="+status+"=msg="+msg);

                if (status == 0 && !TextUtils.isEmpty(data)){
                    AppUpdateBean bean = JsonUtils.getManager().stringToBean(data, AppUpdateBean.class);
                    if (bean != null && bean.isAppUpdate()){
                        //有app更新
                        DialogUtils.getUtils().appUpdate(activity, "有新版本啦, 更新率先使用新功能", bean.getDesc());
                    }

                }
            }
        });
    }


    /**
     * 获取U城主页的热门信息
     * */
    public void getHot(Activity activity, final OnHttpResultListener<UcityInfoBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UNI_ID, UChengApplication.uniId);
//        if (UChengApplication.uniId >0){
//            map.put(BaseConfig.UNI_ID, UChengApplication.uniId);
//        }else {
//            map.put(BaseConfig.UNI_ID, 0);
//        }
        HttpManager.newInstance(activity).queryCheck(UCityConfig.GetHot.method, UCityConfig.GetHot.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "热门活动stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "热门活动stutas="+status+"=msg="+msg);
                if (status == 0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    List<UcityInfoBean> list = JsonUtils.getManager().parseList(data, UcityInfoBean.class);
                    listener.onResult(status, msg, list.size(), list);
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }



    public interface GetUcityInfosListener{
        void onResult(int status, int allPagers,  String msg,  List<UcityInfoBean> data);
    }

    public interface GetUcityInfoListener{
        void onResult(int status, String msg, UcityInfoBean bean);
    }
}
